package com.kdx.wallet.controller;

import com.kdx.wallet.Constant;
import com.kdx.wallet.entity.dto.CommonResponses;
import com.kdx.wallet.service.ProdService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/prod")
public class ProdController {
    @Resource
    private ProdService prodService;

    /**
     * 购买商品
     */
    @ResponseBody
    @RequestMapping("/buy")
    public CommonResponses<String> buyProd(Integer userId, Integer prodId) {
        boolean b = prodService.buyProd(userId, prodId);
        if (b){
            return new CommonResponses<>("success", Constant.SUCCESS_CODE,Constant.SUCCESS_MSG);
        }
        return new CommonResponses<>("failed", Constant.BUY_PROD_FAILED_CODE,Constant.SUCCESS_MSG);
    }

    /**
     * 申请退款
     */
    @ResponseBody
    @RequestMapping("/refund")
    public CommonResponses<String> requestRefund(Integer userId, Integer prodId) {
        boolean b = prodService.requestRefund(userId, prodId);
        if (b) {
            return new CommonResponses<>("success", Constant.SUCCESS_CODE, Constant.SUCCESS_MSG);
        }
        return new CommonResponses<>("failed", Constant.REFUND_FAILED_CODE, Constant.FAILED_MSG);
    }

}
